import Mock from 'mockjs';

import setupMock from '@/utils/setupMock'

const Random = Mock.Random;

const data = {
  "tags": [
    "4年前端开发经验"
  ],
  "showPosition": [
    "文章",
    "文章详情"
  ],
  "createTime": 1598256829,
  "updateTime": 1600164867,
  "_id": "5f4376bd94c942f8bc6daab1",
  "friendLink": [
    {
      "_id": "5f4376bd94c942f8bc6daab2",
      "link": "https://xuwenliu.github.io/",
      "icon": "github"
    },
    {
      "_id": "5f4376bd94c942f8bc6daab3",
      "link": "https://segmentfault.com/u/xuwenliu/answers",
      "icon": "sf"
    },
    {
      "_id": "5f4376bd94c942f8bc6daab4",
      "link": "https://www.zhihu.com/people/xu-wen-liu-83/posts",
      "icon": "zhihu"
    }
  ],
  "nickName": "NeverGiveUpT",
  "desc": "专注于WEB和移动前端开发"
}

const adData = {
  "showPosition": [
    "文章",
    "文章详情"
  ],
  "createTime": 1598257275,
  "updateTime": 0,
  "_id": "5f43787b94c942f8bc6daac1",
  "imgs": [
    {
      "_id": "5f43787b94c942f8bc6daac2",
      "imgUrl": "https://img.alicdn.com/tfs/TB1v0eeB4z1gK0jSZSgXXavwpXa-2880-574.png",
      "link": "https://www.aliyun.com/minisite/goods?userCode=h55rc1yh"
    }
  ]
}

const { list } = Mock.mock({
  'list|100': [
    {
      _id: /[0-9]{8}[-][0-9]{4}/,
      name: () =>
        Random.pick([
          '每日推荐视频集',
          '抖音短视频候选集',
          '国际新闻集合',
        ]),
      cover: Random.image('200x100', '#FF6600'),
      createTime: Random.datetime(),
      "showPosition": [
        "文章",
        "文章详情"
      ],
      "isVip": true,
      "updateTime": Random.datetime(),
      "project": "2",
      "platform": Mock.mock('@cname'),
      "link": "https://www.iqiyi.com/v_2g5a5i86730.html?vfrm=pcw_home&vfrmblk=B&vfrmrst=fcs_0_p11"
    },
  ],
});
setupMock({
  setup() {
    Mock.mock(new RegExp('/api/site/right/introduction'), (params) => {
      console.log('---', params);

      switch (params.type) {

        case 'PUT':
          const body = JSON.parse(params.body);
          return {
            "msg": "个人简介修改成功",
            "data": body,
            "code": 200
          }
        case 'POST':
          const postBody = JSON.parse(params.body);
          return {
            "msg": "个人简介添加成功",
            "code": 200,
            data: postBody
          }
        case 'GET':
        default:
          return {
            msg: "个人简介获取成功",
            code: 200,
            data
          }
      }



    })
    Mock.mock(new RegExp('/api/site/right/ad'), (params) => {
      console.log('---', params);

      switch (params.type) {

        case 'PUT':
          const body = JSON.parse(params.body);
          return {
            "msg": "广告设置修改成功",
            "data": body,
            "code": 200
          }
        case 'POST':
          const postBody = JSON.parse(params.body);
          return {
            "msg": "广告设置添加成功",
            "code": 200,
            data: postBody
          }
        case 'GET':
        default:
          return {
            msg: "广告设置获取成功",
            code: 200,
            data: adData
          }
      }



    })
    Mock.mock(new RegExp('/api/site/right/recommend'), (params) => {
      console.log('---', params);

      switch (params.type) {
        case 'DELETE':
          const delBody = JSON.parse(params.body);
          const idx = list.findIndex(item => item._id === delBody._id);
          list.splice(idx, 1);
          return {
            "msg": "删除成功",
            "data": null,
            "code": 200
          }
        case 'PUT':
          const body = JSON.parse(params.body);
          const index = list.findIndex(item => item._id === body._id);
          list[index] = { ...list[index], ...body };
          return {
            "msg": "修改成功",
            "data": body,
            "code": 200
          }
        case 'POST':

          const postBody = JSON.parse(params.body);
          list.unshift(postBody);

          return {
            "msg": "添加成功",
            "code": 200,
            data: postBody
          }
        case 'GET':
        default:
          const pageDate = JSON.parse(params.body)
          const p = pageDate.page as number;
          const ps = pageDate.pageSize as number;

          return {
            code: 200,
            msg: '请求成功',
            data: {
              list: list.slice((p - 1) * ps, p * ps),
              total: list.length,
            }
          };
      }
    })
  },
});
